package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.sgb.kjwl.view.ui.adapter.self.publish.UnitAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class UnitPopWindow extends BasePopWindow {

    private RecyclerView recyclerView;
    private TextView okView;
    private TextView resetView;
    private UnitAdapter unitAdapter;
    private TextView titleTv;
    private LinearLayout noDataLay;
    private List<UnitEntity> unitEntityList;
    private String no = "";
    private String name = "";
    private LinearLayout linearLayout;
    private EditText editText;
    private InputTextInterface inputTextInterface;

    public UnitPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_storage;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.skill_list);
        titleTv = holder.getPop().findViewById(R.id.title);
        okView = holder.getPop().findViewById(R.id.ok_view);
        noDataLay = holder.getPop().findViewById(R.id.ll_empty);
        editText = holder.getPop().findViewById(R.id.et_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        linearLayout = holder.getPop().findViewById(R.id.ll_add);
        editText.setHint("搜索单位");
        titleTv.setText("选择单位");
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(no, name, null);
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        linearLayout.setVisibility(View.GONE);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    inputTextInterface.input(editText.getText().toString().trim());
                }
                return false;
            }
        });
        unitAdapter = new UnitAdapter(mContext, null);
        unitAdapter.setItemSelect(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(unitAdapter);
        unitAdapter.setOnItemClick(new AdapterOnItemClick<UnitEntity>() {
            @Override
            public void onItemClick(UnitEntity unitEntity, int position) {
//                no = unitEntity.get();
                name = unitEntity.getName();
            }
        });
    }

    public void setData(List<UnitEntity> unitEntityList,boolean isUpdate) {
        this.unitEntityList = unitEntityList;
        unitAdapter.setDatas(unitEntityList);
        if (isUpdate) {
            unitAdapter.setDefItem(-1);
        }
        if (unitEntityList.isEmpty()){
            noDataLay.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }else {
            noDataLay.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
    }

    public interface InputTextInterface {
        void input(String s);
    }

    public void inputTextInterface(InputTextInterface inputTextInterface) {
        this.inputTextInterface = inputTextInterface;
    }

}
